const {ccclass, property} = cc._decorator;
@ccclass
export default class qxc_Node {
    //获取两点之间距离=============================================================
    //获取两个 坐标 之间的长度
    static getPosToPosLength(pos1: cc.Vec2 | cc.Vec3, pos2: cc.Vec2 | cc.Vec3): number {
        return cc.v2(pos2.x - pos1.x, pos2.y - pos1.y).mag();
    }
    //获取 node 和 坐标 之间的长度
    static getNodeToPosLength(node: cc.Node,targetPos: cc.Vec2 | cc.Vec3){
        let pos: cc.Vec2 = node.convertToWorldSpaceAR(cc.v2());
        return this.getPosToPosLength(pos, targetPos);
    }
    //获取两个 node 之间的长度
    static getNodeToNodeLength(node: cc.Node, targetNode: cc.Node): number {
        let pos1: cc.Vec2 = node.convertToWorldSpaceAR(cc.v2());
        let pos2: cc.Vec2 = targetNode.convertToWorldSpaceAR(cc.v2());
        return this.getPosToPosLength(pos1, pos2);
    }
    //获取两个物体之前的角度 弧度=============================================================
    // 弧度转角度
    static radianToAngle(radian:number){
        let angel = radian / Math.PI * 180;
        return angel;
    }
    // 角度转弧度
    static angelToRadian(angel:number){
        let radian = Math.PI * angel / 180;
        return radian;
    }
    //获取两个节点之间的弧度
    static getTowRadian(node:cc.Node,target:cc.Node):number{
        let y = target.convertToWorldSpaceAR(cc.v2(0,0)).y - node.convertToWorldSpaceAR(cc.v2(0,0)).y;
        let x = target.convertToWorldSpaceAR(cc.v2(0,0)).x - node.convertToWorldSpaceAR(cc.v2(0,0)).x;
        let radian:number = Math.atan2(y,x); //Math.atan2(y,x)获取弧度 这里 y x 填的是相对坐标
        return radian;
    }
    //获取节点和座标之间的弧度
    static getNodeToPosRadian(node:cc.Node,pos: cc.Vec2 | cc.Vec3){
        let y = pos.y - node.convertToWorldSpaceAR(cc.v2(0,0)).y;
        let x = pos.x - node.convertToWorldSpaceAR(cc.v2(0,0)).x;
        //Math.atan2(y,x)获取弧度 这里 y x 填的是相对坐标
        let radian = Math.atan2(y,x);
        return radian;
    }
    //获取节点和座标之间的角度
    static getNodeWithPosAngle(node:cc.Node,pos: cc.Vec2 | cc.Vec3){
        let radian = this.getNodeToPosRadian(node,pos);
        let angle = this.radianToAngle(radian);
        return angle;
    }
    // 获取两个节点之间的角度
    static getTowAngle(node:cc.Node,target:cc.Node){
        let radian = this.getTowRadian(node,target);
        let angle = this.radianToAngle(radian);
        return angle;
    }
    //node移动到目标=================================================================
    //node 到 node
    static nodeToNode(node:cc.Node,targetNode:cc.Node){
        let x = targetNode.convertToWorldSpaceAR(cc.v2(0,0)).x - node.convertToWorldSpaceAR(cc.v2(0,0)).x;
        let y = targetNode.convertToWorldSpaceAR(cc.v2(0,0)).y - node.convertToWorldSpaceAR(cc.v2(0,0)).y;
        node.setPosition(x,y);
    }
    //(update)node 移动到 node
    static nodeMoveToNodeUpDate(node:cc.Node,targetNode:cc.Node,speed:number = null,time:number = null){
        if(time == null) time = 0.1;
        if(speed == null) speed = 3;
        let radian = this.getTowRadian(node,targetNode);
        let x = Math.cos(radian) * speed;
        let y = Math.sin(radian) * speed;
        node.runAction(cc.moveBy(time,cc.v2(x,y)))
    }
    //(Tween)node 移动到 node
    static nodeMoveToNodeTween(node:cc.Node,targetNode:cc.Node,time:number = 1){
        let targetX = targetNode.convertToWorldSpaceAR(cc.v2(0,0)).x - node.convertToWorldSpaceAR(cc.v2(0,0)).x;
        let targetY = targetNode.convertToWorldSpaceAR(cc.v2(0,0)).y - node.convertToWorldSpaceAR(cc.v2(0,0)).y;
        cc.tween(node).to(time,{x:targetX,y:targetY}).start();
    }
    //(update)节点移动到目标座标
    static nodeMoveToPosUpDate(node:cc.Node,pos: cc.Vec2 | cc.Vec3,speed:number = null,time:number = null){
        if(time == null) time = 0.1;
        if(speed == null) speed = 3;
        let radian = this.getNodeToPosRadian(node,pos);
        let x = Math.cos(radian) * speed;
        let y = Math.sin(radian) * speed;
        node.runAction(cc.moveBy(time,cc.v2(x,y)))
    }

    //节点朝向=====================================================================
    // 节点朝向目标节点
    static nodeFaceToNode(node:cc.Node,targetNode:cc.Node,jiaoZheng:number = 0){
        let twoAngle = this.getTowAngle(node,targetNode);
        node.angle = twoAngle + jiaoZheng;
    }
    static nodeFaceToPos(node:cc.Node,pos: cc.Vec2 | cc.Vec3,jiaoZheng:number = 0){
        let twoAngle = this.getNodeWithPosAngle(node,pos);
        node.angle = twoAngle + jiaoZheng;
    }
}


